<?php

/**
 * Parse the rankings pages of Mikubook.com
 */
class Mikubook_Video_Ranking_Parser {
	
	
	var $htmlcontent;
	
	public function __construct($htmlcontent) {
		$this->htmlcontent = $htmlcontent;
	}
	
	/**
	 * Get a top 20 type (day, week, all)
	 */
	public function getTop20($type = "day") {
		$xml = new DOMDocument();
		
		error_reporting(E_ERROR | E_PARSE);
		$xml->loadHTML($this->htmlcontent);
		
		$classnames = array(
			"day" => "rank_daily",
			"week" => "rank_weekly",
			"all" => "rank_total"
		);
		
		$dailyBlocks = DOM_Helper::getElementsByClassName($xml, "div", $classnames[$type]);
		
		$vocaVideoBlock = DOM_Helper::getNextElementSibling($dailyBlocks[0]);
		$videos = array();
		while (DOM_Helper::hasClassName($vocaVideoBlock, "item") && !DOM_Helper::hasClassName($vocaVideoBlock, "ranking_title")) {
			$video = $this->parseVideoBlock($vocaVideoBlock);
			$vocaVideoBlock = DOM_Helper::getNextElementSibling($vocaVideoBlock);
			$videos[] = $video;
		}
		return $videos;	
	}
	
	/**
	 * Get the title of the video in the DOM
	 */
	private function video_getTitle($videoBlock) {
		$elems = DOM_Helper::getElementsByClassName($videoBlock, "div", "i_title");
		$e = $elems[0];
		$title = trim(html_entity_decode($e->textContent));
		return $title;
	}

	/**
	 * Get the id of the video in the DOM
	 */
	private function video_getID($videoBlock) {
		$elems = DOM_Helper::getElementsByClassName($videoBlock, "input", "hdnitemid");
		$e = $elems[0];
		$id = $e->getAttribute("value");
		return $id;
	}
	
	/**
	 * Get the thume images of the video in the DOM
	 */
	private function video_getImageLink($videoBlock) {
		$elems = DOM_Helper::getElementsByClassName($videoBlock, "div", "i_thum");
		$e = $elems[0];
		$imgs = $e->getElementsByTagName("img");
		$img = $imgs->item(1);
		$href = $img->getAttribute("src");
		return $href;
	}
	
	/**
	 * Parse a block video and return a Mikubook_Video object
	 */
	private function parseVideoBlock($video) {
		$o = array(
			'title' 	=> $this->video_getTitle($video),
			'id' 		=> $this->video_getID($video),
			'image' 	=> $this->video_getImageLink($video)
		);

		return new Mikubook_Video($o);
	}
}


/**
 * Parse the "newest" video page of Mikubook.com
 */
class Mikubook_Video_Newest_Parser {
	static $URL_HTML_NEW = "http://mikubook.com/home/new";
	static $URL_NEW = "http://mikubook.com/top/addMovieWizUser";
	
	var $htmlcontent;
	
	public function __construct($htmlcontent) {
		$this->htmlcontent = $htmlcontent;
	}
	
	/**
	 * Get the newest videos
	 * NOTE: Offset is broken in original site
	 */
	public function getNewest($offset = 0) {
	
		if ($offset == 0) { // We need to get the html page to get the first newest video
			$videos = array();
			$xml = new DOMDocument();
			
			error_reporting(E_ERROR | E_PARSE);
			$xml->loadHTML($this->htmlcontent);
			$videosBlocks = DOM_Helper::getElementsByClassName($xml, "div", "item");
			foreach ($videosBlocks as $item) {
				$elems = DOM_Helper::getElementsByClassName($item, "input", "hdnitemid");
				$e = $elems[0];
				$id = $e->getAttribute("value");
				$videos[] = new Mikubook_Video(array(
					'id' => $id
				));
			}
		}
		else {
			$objects = json_decode($this->htmlcontent);
			$videos = array();
			foreach ($objects as $o) {
				$v = new Mikubook_Video(array(
					'title' => $o->title,
					'link' => $o->url,
					'image' => $o->thumbnail,
					'id' => $o->id,
					'favecount' => $o->favecount,
					'rome' => $o->rome
				));
				$videos[] = $v;
			}
		}
		return $videos;
	}
	
	/**
	 * Get the url for the newest videos
	 */
	public static function getUrl($offset = 0) {
		if (empty($offset)) {
			$offset = 0;
		}
		if ($offset == 0) {
			$url = self::$URL_HTML_NEW;
		}
		else {
			$url = self::$URL_NEW."?offset=$offset";
		}
		return $url;
	}
	
}

/**
 * Parse the tag videolist webservice of Mikubook.com
 */
class Mikubook_Video_Tag_Parser {
	
	static $URL_TAG = "http://mikubook.com/search/addTagStream?category=Videos";
	
	var $htmlcontent;
	
	public function __construct($htmlcontent) {
		$this->htmlcontent = $htmlcontent;
	}
	
	/**
	 * Obtain the videos of a tag
	 */
	public function getByTag() {
		$objects = json_decode($this->htmlcontent);
		$videos = array();
		foreach ($objects as $o) {
			$v = new Mikubook_Video(array(
				'title' => $o->title,
				'link' => $o->url,
				'image' => $o->thumbnail,
				'id' => $o->id,
				'favecount' => $o->favecount,
				'rome' => $o->rome
			));
			$videos[] = $v;
		}
		return $videos;
	}
	
	/**
	 * Get the url for the webservice with a tagname, offset and order
	 */
	public static function getUrl($tagname = "", $offset = 0, $typeOrder = "newest") {
		
		$order = 0;
		if (empty($offset)) {
			$offset = 0;
		}
		switch($typeOrder) {
			
			case "popular":
				$order = 2;
				break;
				
			case "newest":
			default:
				$order = 1;
				break;
		}
		
		$url = self::$URL_TAG."&tag=".rawurlencode($tagname)."&order=$order&offset=$offset";
		return $url;
	}
	
}


/**
 * Parse the video search webservice of Mikubook.com
 */
class Mikubook_Video_Search_Parser {

	static $URL_SEARCH = "http://mikubook.com/search/addVideosStream?";
	
	var $htmlcontent;
	
	public function __construct($htmlcontent) {
		$this->htmlcontent = $htmlcontent;
	}
	
	/**
	 * Get the videos with keywords
	 */
	public function getBySearch() {
		$objects = json_decode($this->htmlcontent);
		$videos = array();
		foreach ($objects as $o) {
			$v = new Mikubook_Video(array(
				'title' => $o->title,
				'link' => $o->url,
				'image' => $o->thumbnail,
				'id' => $o->id,
				'favecount' => $o->favecount,
				'rome' => $o->rome
			));
			$videos[] = $v;
		}
		return $videos;
	}
	
	/**
	 * Get the URL for the webservice with words, offset and sort
	 */
	public static function getUrl($words = "", $offset = 0, $typeOrder = "newest") {
		
		$order = 0;
		if (empty($offset)) {
			$offset = 0;
		}
		switch($typeOrder) {
			
			case "popular":
				$order = "content_favecount";
				break;
				
			case "newest":
			default:
				$order = "created_at";
				break;
		}
		
		$url = self::$URL_SEARCH."words=".rawurlencode($words)."&sort=$order&offset=$offset";
		return $url;
	}

}